<template>
  <div class="content">
    <div class="header">
      <h3 class="title">{{ contentConfig?.header?.title ?? '数据列表' }}</h3>
      <el-button v-if="isCreate" type="primary" @click="handleNewUserClick">
        {{ contentConfig.header?.btnTitle ?? '新建数据' }}
      </el-button>
    </div>
    <div class="table">
      <el-table
        :data="pageList"
        border
        style="width: 100%"
        v-bind="contentConfig.childrenTree"
      >
        <template v-for="item in contentConfig.propsList" :key="item.prop">
          <template v-if="item.type == 'timer'">
            <el-table-column align="center" v-bind="item">
              <template #default="scoped">
                {{ formatUTC(scoped.row[item.prop]) }}
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type == 'handler'">
            <el-table-column align="center" fixed="right" v-bind="item">
              <template #default="scoped">
                <el-button
                  text
                  icon="Edit"
                  type="primary"
                  size="small"
                  @click="handleEditBtnClick(scoped.row)"
                  v-if="isUpdate"
                >
                  编辑
                </el-button>
                <el-button
                  @click="handleDelectBtnClick(scoped.row.id)"
                  text
                  icon="Delect"
                  type="danger"
                  size="small"
                  v-if="isDelete"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type == 'custom'">
            <el-table-column align="center" v-bind="item">
              <template #default="scoped">
                <slot
                  :name="item.slotName"
                  v-bind="scoped"
                  :prop="item.prop"
                ></slot>
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column align="center" v-bind="item" />
          </template>
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50, 100]"
        :total="pageTotalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="content">
import { formatUTC } from '@/utils/format'
import { storeToRefs } from 'pinia'
import userSystemStore from '@/store/main/system/system'
import { ref } from 'vue'
import usePromissions from '@/hooks/usePermissions'
interface IProps {
  contentConfig: {
    pageName: string
    header?: {
      title?: string
      btnTitle?: string
    }
    propsList: any[]
    childrenTree: any
  }
}
const props = defineProps<IProps>()
// 自定义事件
const emit = defineEmits(['newClick', 'editClick'])
// 获取页面是否存在对应的增删改查权限
const isCreate = usePromissions(`${props.contentConfig.pageName}:create`)
const isDelete = usePromissions(`${props.contentConfig.pageName}:delete`)
const isUpdate = usePromissions(`${props.contentConfig.pageName}:update`)
const isQuery = usePromissions(`${props.contentConfig.pageName}:query`)

const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = userSystemStore()
fetchPageListData()
// 展示列表数据
const { pageList, pageTotalCount } = storeToRefs(systemStore)

// 分页逻辑
const handleCurrentChange = () => {
  fetchPageListData()
}
const handleSizeChange = () => {
  fetchPageListData()
}
// 触发网络请求
function fetchPageListData(formData = {}) {
  if (!isQuery) return
  // 请求参数
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  // 请求用户列表数据
  const queryInfo = { ...pageInfo, ...formData }
  systemStore.postPageListAction(props.contentConfig.pageName, queryInfo)
}

// 删除功能
function handleDelectBtnClick(id: number) {
  systemStore.delectPageByIdAction(props.contentConfig.pageName, id)
}
// 编辑功能
function handleEditBtnClick(row: any) {
  emit('editClick', row)
}

// 新建功能
function handleNewUserClick() {
  emit('newClick')
}
// 监听store的action中的操作
// {
//   name, // action 名称
//   store, // store 实例，类似 `someStore`
//   args, // 传递给 action 的参数数组
//   after, // 在 action 返回或解决后的钩子
//   onError, // action 抛出或拒绝的钩子
// }

systemStore.$onAction(({ name, after }) => {
  console.log('action被调用了', name)
  after(() => {
    if (
      name === 'deletePageByIdAction' ||
      name === 'editPageDataAction' ||
      name === 'newPageDataAction'
    ) {
      currentPage.value = 1
    }
  })
})
defineExpose({
  fetchPageListData
})
</script>

<style lang="less" scoped>
.content {
  background: #fff;
  margin-top: 20px;
  padding: 20px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 10px;
    .title {
      font-size: 22px;
    }
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
